package com.bgds.sys.module.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.bgds.sys.api.service.BaoXinService;
import com.bgds.sys.common.excel.EasyExcelExcelUtil;
import com.bgds.sys.common.pagevo.DeviceFaultExcel;
import com.bgds.sys.common.pagevo.PageVo;
import com.bgds.sys.common.pagevo.Result;
import com.bgds.sys.common.utils.DateUtils;
import com.bgds.sys.common.utils.ResultUtil;
import com.bgds.sys.module.entity.DeviceFault;
import com.bgds.sys.module.entity.dto.DeviceFaultDto;
import com.bgds.sys.module.service.IDeviceFaultService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  设备故障前端控制器
 * </p>
 *
 * @author lj
 * @since 2020-05-13
 */
@RestController
@RequestMapping("/deviceFault")
@Api(tags = "设备故障")
public class DeviceFaultController {

    @Autowired
    private IDeviceFaultService iDeviceFaultService;
    @Autowired
    private BaoXinService baoXinService;



    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ApiOperation(value = "保存设备故障",notes = "保存设备故障",httpMethod = "POST")
    public Result<Object> save(@RequestBody DeviceFault deviceFault){
        deviceFault.setCreateTime(System.currentTimeMillis());

        Boolean flag= iDeviceFaultService.saveOrUpdate(deviceFault);
        if(flag){
            return new ResultUtil<Object>().setSuccessMsg("保存成功");
        }
        return new ResultUtil<Object>().setSuccessMsg("保存失败");
    }
    @RequestMapping(value = "/refresh", method = RequestMethod.GET)
    @ApiOperation(value = "刷新设备故障",notes = "刷新设备故障",httpMethod = "GET")
    public Result<Object> refresh(){
       List<DeviceFault> deviceFaults= baoXinService.getDeviceFaultData();
        Boolean flag=iDeviceFaultService.saveBatch(deviceFaults);
        if(flag){
            return new ResultUtil<>().setSuccessMsg("刷新成功");
        }
        return new ResultUtil<>().setErrorMsg("刷新失败");
    }
    @RequestMapping(value = "/listByPage", method = RequestMethod.GET)
    @ApiOperation(value = "分页获取设备故障",notes = "分页获取设备故障",httpMethod = "GET",response = Result.class)
    public Result<IPage<DeviceFault>> listByPage(@ModelAttribute PageVo page, Long startTime,Long endTime){
        IPage<DeviceFault> iPage=  iDeviceFaultService.listByParmas(page,startTime,endTime);
        return new ResultUtil<IPage<DeviceFault>>().setData(iPage);
    }

    @SneakyThrows
    @RequestMapping(value = "/exportExcel", method = RequestMethod.GET)
    @ApiOperation(value = "导出设备故障",notes = "导出设备故障",httpMethod = "GET")
    public void exportExcel( HttpServletResponse response,Long startTime,Long endTime){
        List<DeviceFault> list= iDeviceFaultService.getDviceFaults(startTime,endTime);
        List<DeviceFaultExcel> deviceFaultExcels=new ArrayList<>();
        for(DeviceFault df:list){
            DeviceFaultExcel dfe=new DeviceFaultExcel();
            dfe.setBlameBy(df.getBlameBy());
            dfe.setCreateBy(df.getCreateBy());
            if(df.getCreateTime()!=null){
               Date createTime= new Date();
                createTime.setTime(df.getCreateTime());
                dfe.setCreateTime(DateUtils.convertDate2String("yyyy-MM-dd HH:mm:ss",createTime));
            }
            if(df.getFaultStartTime()!=null){
                Date start= new Date(df.getFaultStartTime());
                dfe.setFaultStartTime(DateUtils.convertDate2String("yyyy-MM-dd HH:mm:ss",start));
            }
            if(df.getFaultEndTime()!=null){
                Date end= new Date(df.getFaultEndTime());
                dfe.setFaultEndTime(DateUtils.convertDate2String("yyyy-MM-dd HH:mm:ss",end));
            }
            dfe.setDuration(df.getDuration());
            dfe.setFaultAttr(df.getFaultAttr());
            dfe.setFaultContent(df.getFaultContent());
            dfe.setFaultName(df.getFaultName());
            dfe.setMeasures(df.getMeasures());
            dfe.setOperationLine(df.getOperationLine());
            dfe.setReason(df.getReason());
            dfe.setRemarks(df.getRemarks());
            dfe.setSpecialityAttr(df.getSpecialityAttr());
            deviceFaultExcels.add(dfe);
        }

        EasyExcelExcelUtil.downLoadExecl(response,"设备故障",deviceFaultExcels,DeviceFaultExcel.class);

    }


}

